//基类控制器
layui.define(['jquery', 'conf', 'api', 'func', 'table', 'form'], function (exports) {
    const {jquery: $, conf, api, func, table, form} = layui;

    exports('action', {
        config: {
            //默认请求同地址接口获取初始化数据
            assign: true,

        },
        //url入参信息
        param: {},
        //同路径请求地址返回结果data信息
        assign: {
            entity: {},
        },
        //自定义事件绑定
        binder: [],
        //自定义属性
        data: {},
        //窗口变动触发事件
        resize: function () {

        },
        //渲染前执行
        beforeRender: function () {
            return new Promise(function (resolve, reject) {
                resolve();
            });
        },
        //渲染成功后执行
        afterRender: function () {

        },
        //编辑表格字段回调
        editTableField: function () {

        },
        //接收到的数据
        receiveMessage: function (from, data) {

        },
        //基类方法
        methods: {
            //表单编辑页
            form: function (elem) {
                const that = this;

                var cancel = false;
                const title = $(elem).text();
                const id = $(elem).data('id') || 0;

                var urlArray = layui.router().href.split("/");
                urlArray.pop();
                urlArray.push(arguments.callee.name);
                var url = ($(elem).data("url") || urlArray.join('/'));

                var data = $(elem).data();
                data = func.snake2camel(data);
                url += "?" + $.param(data);

                var layArea = $(elem).attr("lay-area");
                if (!layArea) {
                    layArea = ['95%', '95%'];
                } else {
                    layArea = eval(layArea);
                }
                var endFn = $(elem).attr("lay-end");

                if (!endFn) {
                    endFn = function () {
                        if (!cancel) {
                            setTimeout(function () {
                                that.search();
                            }, 250);
                        }
                    };
                } else {
                    endFn = that[endFn];
                }

                parent.layer.open({
                    title: title,
                    type: 2,
                    content: func.url(url),
                    area: layArea,
                    offset: 't',
                    end: endFn,
                    cancel: function () {
                        cancel = true;
                    },
                })
            },
            //layer iframe
            iframe: function (elem) {
                const that = this;
                try {
                    var cancel = false;

                    const title = $(elem).text();
                    const url = $(elem).attr('lay-url');
                    const offset = $(elem).attr("lay-offset") || 'auto';
                    var area = $(elem).attr("lay-area");
                    if (!area) {
                        area = ['90%', '90%'];
                    } else {
                        area = eval(area);
                    }

                    var endFn = $(elem).attr("lay-end");
                    if (!endFn) {
                        endFn = function () {
                            if (!cancel) {
                                setTimeout(function () {
                                    that.search();
                                }, 250);
                            }
                        };
                    } else {
                        endFn = that[endFn];
                    }

                    console.log(func.url(url, $(elem).data()));
                    parent.layer.open({
                        title: title,
                        type: 2,
                        content: func.url(url, $(elem).data()),
                        area: area,
                        offset: offset,
                        end: endFn,
                        cancel: function () {
                            cancel = true;
                        },
                    });
                } catch (e) {
                    parent.layer.msg('lay-iframe')
                }
            },
            //表单提交
            submit: function (data) {
                let beforeMethod = $(data.elem).data("before");
                if (beforeMethod) {
                    var result = this[beforeMethod](data.field);
                    if (result) {
                        data.field = result;
                    }
                }

                const success = $(data.elem).data("success") || function (res) {
                    parent.layer.msg(res.msg);
                    if (res.code === conf.api.code.success) {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                        setTimeout(function () {
                            location.reload();
                        }, 1000);
                    }
                };
                const error = $(data.elem).data("error") || function () {
                    parent.layer.msg('网络错误');
                }

                const url = $(data.elem).data("url");
                const type = $(data.elem).data("type") || 'post';

                api(url, data.field, type).then(success).catch(error);
            },
            //表格刷新
            search: function (data, pageCurr, tableTarget) {
                if (!tableTarget) {
                    tableTarget = Object.keys(layui.table.cache).shift();
                }
                if (!data) {
                    data = {field: form.getValue(null, $("form.layui-form"))};
                }

                pageCurr = $(data.elem).data("page") || pageCurr;

                table.reload(tableTarget, {
                    page: {
                        curr: pageCurr || $('.layui-laypage-em').next().text() //重新从第 1 页开始
                    },
                    where: data.field || {}
                }, 'data');
            },

            del: function (elem) {
                const that = this;
                var id = $(elem).data('id');
                if (!id) {
                    const idField = $(elem).attr("lay-field");
                    var tableTarget = $(elem).attr('lay-table');
                    //批量操作
                    if (!tableTarget) {
                        tableTarget = Object.keys(layui.table.cache).shift();
                    }
                    id = func.tableCheckBoxFields(tableTarget, idField ? idField : 'id');
                    if (!id) {
                        return;
                    }
                }

                var urlArray = layui.router().href.split("/");
                urlArray.pop();
                urlArray.push(arguments.callee.name);
                var url = ($(elem).attr("lay-url") || urlArray.join('/'));
                parent.layer.confirm('确认删除吗？', {title: false, closeBtn: 0}, function (index) {
                    api(url, {id: id}).then(function (res) {
                        parent.layer.msg(res.msg);
                    });
                    parent.layer.close(index);

                    setTimeout(function () {
                        that.search();
                    }, 500);
                });
            },
            status: function (elem) {
                const that = this;
                var id = $(elem).data('id');
                if (!id) {
                    const idField = $(elem).attr("lay-field");
                    var tableTarget = $(elem).attr('lay-table');
                    //批量操作
                    if (!tableTarget) {
                        tableTarget = Object.keys(layui.table.cache).shift();
                    }
                    id = func.tableCheckBoxFields(tableTarget, idField ? idField : 'id');
                    if (!id) {
                        return;
                    }
                }

                const data = $(elem).data();
                data.id = id;


                var urlArray = layui.router().href.split("/");
                urlArray.pop();
                urlArray.push(arguments.callee.name);
                var url = ($(elem).attr("lay-url") || urlArray.join('/'));
                parent.layer.confirm('确认' + $(elem).text().trim() + '吗？',
                    {title: false, closeBtn: 0},
                    function (index) {
                        api(url, data).then(function (res) {
                            parent.layer.msg(res.msg);
                        });
                        parent.layer.close(index);
                        setTimeout(function () {
                            that.search();
                        }, 500);
                    }
                );
            },
        },

    });
});
